package com.xiaoshuidi.cloud.module.rooms.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.rooms.api.home.vo.RoomLayoutVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.grounding.vo.GroundingVo;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.pricing.vo.RoomHousePricingPageResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomDevicePageVO;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.pojo.dto.roomhouse.HouseDisperseFilingsDTO;
import com.xiaoshuidi.cloud.module.rooms.pojo.dto.roomhouse.RoomHouseFilingsListDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RoomHouseMapper extends BaseMapperX<RoomHouse> {

    @Select("select distinct(layout_id) from room_house where ${ew.sqlSegment}")
    List<RoomHouse> distinctLayoutId(@Param("ew") LambdaQueryWrapper<?> wrapper);

    @Select("select rh.id           id, " +
            "       rh.no  no ," +
            "       rh.house_no     houseNo, " +
            "       rh.name  houseName, " +
            "       rh.building_no buildingNo, " +
            "       rh.area        area, " +
            "       rh.unit_no     unitNo, " +
            "       rh.rent_status rentStatus, " +
            "       rh.month_rental monthRental, " +
            "       rh.apartment_id apartmentId," +
            "       rht.bed_room_num bedRoomNum, " +
            "       living_room_num  livingRoomNum, " +
            "       toilet_num toiletNum " +
            "from room_house rh " +
            "    left join room_house_type rht on rh.room_house_type_id = rht.id  " +
            "where ${ew.sqlSegment}")
    Page<RoomHousePricingPageResp> pricingPage(@Param("ew") QueryWrapper<?> wrapper, Page<?> page);

    /**
     * 集中式房源备案查询列表
     *
     * @param wrapper
     * @return
     */
    @Select(" select rh.id houseId, rh.house_no houseNo, rh.building_no buildingNo, rh.area area, rhf.id houseFilingsId," +
            " rh.tenant_id tenantId  " +
            " from room_house rh " +
            " left join room_house_filings rhf on rh.id = rhf.room_house_id " +
            " where ${ew.sqlSegment}")
    List<RoomHouseFilingsListDTO> filingsHouseList(@Param("ew") Wrapper<?> wrapper);

    @Select("select rh.id                id, " +
            "       rh.super_id          superId, " +
            "       rh.name              roomName, " +
            "       rhi.owner_name       ownerName, " +
            "       rhi.card_type        cardType, " +
            "       rhi.card_no          cardNo, " +
            "       rhi.tel_phone        telPhone, " +
            "       rhi.property_type    propertyType, " +
            "       rhi.property_no      propertyNo, " +
            "       rh.district          region, " +
            "       rh.street            streetName, " +
            "       rh.area              itemName, " +
            "       raa.building_no      buildNo, " +
            "       rh.house_no          roomNo, " +
            "       rhi.register_type    registerType, " +
            "       raa.floor_num        totalFloor, " +
            "       raa.ground_floor_num aboveFloor, " +
            "       rh.floor_no          houseFloor, " +
            "       rhi.house_structure  houseStructure, " +
            "       rh.space             houseArea, " +
            "       rht.bed_room_num     houseType, " +
            "       rh.toward            houseOrientation, " +
            "       rh.decoration        renovation, " +
            "       rhf.external_house_id externalHouseId, " +
            "       rh.tenant_id          tenantId  " +
            "from room_house rh " +
            "         left join room_house_filings rhf on rh.id = rhf.room_house_id " +
            "         left join room_apartment_area raa on rh.apartment_area_id = raa.id " +
            "         left join room_house_info rhi on rh.id = rhi.room_house_id " +
            "         left join room_house_type rht on rh.room_house_type_id = rht.id" +
            "  where ${ew.sqlSegment}")
    List<HouseDisperseFilingsDTO> disperseFilingsList(@Param("ew") QueryWrapper<?> wrapper);

    @Select("SELECT " +
            "p.* " +
            "FROM " +
            "room_house p " +
            "LEFT JOIN room_state_record p2 ON p.id = p2.room_id  " +
            "WHERE ${ew.sqlSegment}")
    List<RoomHouse> getEmptyRoomByLayoutId(@Param("ew") QueryWrapper<RoomHouse> wrapper);

    List<RoomHouse> getByRoomAreaIdAndIsConcentrated(@Param("roomAreaId") Long roomAreaId, @Param("isConcentrated") boolean isConcentrated);


    @Select(" select  rh.area, rh.name, rh.building_no buildingNo, rh.unit_no unitNo, rh.id, rh.apartment_id apartmentId" +
            " from room_house rh " +
            " where ${ew.sqlSegment} ")
    Page<RoomDevicePageVO> devicePage(@Param("ew") QueryWrapper<?> wrapper, Page<?> page);

    @Select("SELECT " +
            "count(*) " +
            "FROM " +
            "room_house p " +
            "LEFT JOIN room_state_record p2 ON p.id = p2.room_id  " +
            "WHERE ${ew.sqlSegment}")
    Integer isHasEmptyRoom(@Param("ew") QueryWrapper<RoomHouse> wrapper);

    @Select("SELECT " +
            "* " +
            "FROM " +
            "room_house p " +
            "LEFT JOIN room_state_record p2 ON p.id = p2.room_id  " +
            "WHERE ${ew.sqlSegment}")
    List<RoomHouse> isHasEmptyRoomByIds(@Param("ew") QueryWrapper<RoomHouse> wrapper);

    @Select("SELECT DISTINCT " +
            "rh.id, " +
            "rh.month_rental  " +
            "FROM " +
            "room_house rh " +
            "LEFT JOIN room_state_record rsr ON rh.id = rsr.room_id  " +
            "WHERE ${ew.sqlSegment}")
    List<RoomHouse> getByApartmentIdOrderByMonthRental(@Param("ew") QueryWrapper<RoomHouse> apartmentId);

    @Select("select * from (select rh.id           id, " +
            "       ra.name        area, " +
            "       rh.apartment_id apartmentId," +
            " rh.grounding isGrounding," +
            " rh.layout_id roomHouseTypeId," +
            " case when rh.month_rental is null then 0 else ROUND(rh.month_rental, 2) end  monthRental," +
            " CONCAT(rh.area,CONCAT(rh.building_no,'栋'),case when rh.is_concentrated=0 then  CONCAT(rh.unit_no,'单元') else '' end,rh.name)roomName ,"+
            "       rht.name roomTypeName " +
            "from room_house rh " +
            "    left join room_apartment ra on rh.apartment_id = ra.id  " +
            "    left join room_layout rht on rh.layout_id = rht.id  " +
            "where rh.deleted =0 AND (rh.room_type <> 4 OR rh.room_type IS  NULL) and(rh.people_num <> 0 OR rh.people_num IS NULL)  and (rh.is_repair<> 1 OR rh.is_repair IS NULL) )gur where  ${ew.sqlSegment}")
    Page<GroundingVo> getGroundingRoomList(@Param("ew") QueryWrapper<?> wrapper, Page<?> page);

    @Select("select count(1) "+
            "from room_house rh " +
            "    left join room_apartment ra on rh.apartment_id = ra.id  " +
            "    left join room_layout rht on rh.layout_id = rht.id  " +
            "where rh.deleted =0 AND (rh.room_type <> 4 OR rh.room_type IS  NULL) and(rh.people_num <> 0 OR rh.people_num IS NULL) and (rh.is_repair<> 1 OR rh.is_repair IS NULL) and grounding=0")
    Long getUnGroundingNum();
    @Select("select count(1) "+
            "from room_house rh " +
            "    left join room_apartment ra on rh.apartment_id = ra.id  " +
            "    left join room_layout rht on rh.layout_id = rht.id  " +
            "where rh.deleted =0 AND (rh.room_type <> 4 OR rh.room_type IS  NULL) and(rh.people_num <> 0 OR rh.people_num IS NULL) and (rh.is_repair<> 1 OR rh.is_repair IS NULL) and grounding=1")
    Long getGroundingNum();


    @Select({"SELECT t1.id roomId,t1.space,t1.month_rental monthRental,t2.room_house_type_id roomHouseTypeId,t2.name roomHouseTypeName " +
            "FROM room_house t1 LEFT JOIN room_layout t2 ON t1.room_house_type_id = t2.room_house_type_id where t1.deleted=0"})
    List<RoomLayoutVO> queryRoomLayout();
}
